package com.demo.bookstore.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
@Slf4j
public class DBConfig {
    @Bean(initMethod = "init", destroyMethod = "close")
    @ConfigurationProperties("spring.datasource.druid.db1")
    public DataSource db1DataSource() {
        log.info("init db1 dataSource");
        return new DruidDataSource();
    }

    @Bean
    public PlatformTransactionManager db1TxManager(DataSource db1DataSource) {
        log.info("init db1 transaction manager");
        return new DataSourceTransactionManager(db1DataSource);
    }

    @Bean
    public JdbcTemplate db1JdbcTemplate(DataSource db1DataSource){
        log.info("init db1 jdbc template");
        return new JdbcTemplate(db1DataSource);
    }
}
